<?php
namespace Common\Model;
use Common\Model\BaseModel;

class CarModel extends BaseModel
{
	protected $tableName = 'car';
	protected $_validate = array(       //查验提交数据时是否有空数据。
//		array('car_type','require','请选择车证类型!'),
//		array('owner_name','require','亲填写车主姓名！'),
//		array('relation','require','请选择与车主关系！'),
		//array('xingshi_license','require','请上传行驶证照片！'),
		//array('jiashi_license','require','请上传驾驶证照片！'),
//		array('plate_number','require','请填写车牌号码'),
	);

	public function addCar($post)   //添加机动车证申请信息
	{
		$ret = array('status'=>'','info'=>'');
		$data = $this->returnData($post);
		if(!preg_match('/^[\x80-\xff]+[A-Z][0-9a-zA-Z]{5}$/',$data['plate_number'])){
			$ret['status'] = false;
			$ret['info'] = "请填写正确的车牌号码";
			return $ret;
		}
		if($this->create($data))
		{
			$res = null !== $data['id'] ? $this->save($data) : $this->add($data);
			if(false !== $res)
			{
				$ret['status'] = true;
				$ret['info']   = "Success";
			}

			else
			{
				$ret['status'] = false;
				$ret['info']   = $this->getDbError();
			}
		}

		else
		{
			$ret['status'] = false;
			$ret['info']   = $this->getError();
		}
		return $ret;
	}

	public function returnData($post)    //将$post变量进行数据整理
	{
		$return['id'] 				= !empty($post['id']) ? intval($post['id']) : null;
		$return['tab_car_id'] 		= !empty($post['tab_car_id']) ? addslashes($post['tab_car_id']) : $this->createTabCarid();
		$return['apply_time'] 		= !empty($post['apply_time']) ? date('Y-m-d H:i:s',$post['apply_time']) : date('Y-m-d H:i:s');
		$return['status'] 			= !empty($post['status']) ? intval($post['status']) : 1;
		$return['remark'] 			= !empty($post['remark']) ? addslashes($post['remark']) : '';
		$return['staff_id'] 		= !empty($post['staff_id']) ? intval($post['staff_id']) : $this->staff_id;
		$return['car_type'] 		= !empty($post['car_type']) ? intval($post['car_type']) : '';
		$return['owner_name'] 		= !empty($post['owner_name']) ? addslashes($post['owner_name']) : '';
		$return['owner_add'] 		= !empty($post['owner_add']) ? addslashes($post['owner_add']) : '';
		$return['relation'] 		= !empty($post['relation']) ? addslashes($post['relation']) : '';
		$return['brand'] 			= !empty($post['brand']) ? addslashes($post['brand']) : '';
		$return['color']		 	= !empty($post['color']) ? addslashes($post['color']) : '';
		$return['car_photo'] 		= !empty($post['car_photo']) ? addslashes($post['car_photo']) : '';
		$return['xingshi_license']	= !empty($post['xingshi_license']) ? addslashes($post['xingshi_license']) : '';
		$return['jiashi_license'] 	= !empty($post['jiashi_license']) ? addslashes($post['jiashi_license']) : '';
		$return['plate_number'] 	= !empty($post['plate_number']) ? mb_strtoupper(addslashes($post['plate_number'])) : '';
		$return['is_drift'] 	    = !empty($post['is_drift']) ? $post['is_drift'] : 1;
		return $return;
	}

	public function createTabCarid()   //创造机动车证申请表编号
	{
		$tab_car_id = date('YmdHis',time());
		return $tab_car_id;
	}

	public function getList()
	{
		$list = $this->select();
		return $list;
	}
}